﻿#include <atlimage.h>
#include <atltime.h>
#include <conio.h>
#include <iostream>   
#include <Windows.h>
#include <fstream>
#include <cassert>
#include <string>
using   namespace   std;

//截取全屏保存为png
void ScreenShot(int fWidthScreen, int fHeightScreen, int nWidthScreen, int nHeightScreen, int Time_deley)
{
	HDC hDCScreen = ::GetDC(NULL);//首先获取到屏幕的句柄    
	int nBitPerPixel = GetDeviceCaps(hDCScreen, BITSPIXEL);//获取到每个像素的bit数目
	int TotalTime = 0;

	//保存到的文件名
	CString strFileName("日期保存图片\\");
	CreateDirectory((LPCTSTR)strFileName, NULL);

	//创建一个CImage的对象
	CImage m_MyImage;
	//Create实例化CImage，使得其内部的画布大小与屏幕一致
	m_MyImage.Create(nWidthScreen - fWidthScreen, nHeightScreen - fHeightScreen, nBitPerPixel);
	HDC hDCImg = m_MyImage.GetDC();

	while (1)
	{
		//使用bitblt 将屏幕的DC画布上的内容 拷贝到CImage上
		BitBlt(hDCImg, 0, 0, nWidthScreen, nHeightScreen, hDCScreen, fWidthScreen, fHeightScreen, SRCCOPY);
		//获取当前时间，以给图片命名
		CTime t = CTime::GetCurrentTime();
		CString tt = t.Format(_T("%Y-%m-%d_%H-%M-%S"));
		CString StorePackage = t.Format(_T("%d"));
		m_MyImage.Save(strFileName + StorePackage + _T("\\") + tt + _T(".PNG"), Gdiplus::ImageFormatPNG);
		Sleep(Time_deley);
		TotalTime += 1;
		if (TotalTime > 100) { break; }//运行超过多少，则释放一次DC句柄
	}

	//前面调用了GetDC所以需要调用ReleaseDC释放掉
	//详情请参见MSDN
	m_MyImage.ReleaseDC();
	ReleaseDC(NULL, hDCScreen);

}

int main()
{
	ifstream file;
	file.open("参数.txt");

	int nWidthScreen;
	int nHeightScreen;
	int fWidthScreen;
	int fHeightScreen;
	int Time_deley;
	file >> fWidthScreen >> fHeightScreen >> nWidthScreen >> nHeightScreen >> Time_deley;
	cout << "起始点为：" << fWidthScreen << ';' << fHeightScreen << "终点为：" << nWidthScreen << ';' << nHeightScreen << ";延时截屏" << Time_deley << endl;
	cout << "请不要关闭此程序！！" << endl;

	while (1)
	{
		ScreenShot(fWidthScreen, fHeightScreen, nWidthScreen, nHeightScreen, Time_deley);//屏幕宽，长，延迟多长时间截取一次屏幕
		cout << "重新开始截屏" << endl;
		Sleep(30000);//休眠三十秒重新启动
	}
}




